package Demo_0422;

/**
 * @author:zhengyilong
 * @email:1797344574@qq.com
 * @phone:13479394730
 * @desc:
 * @DateTime:2022/4/22 11:03
 **/
public class CSqQueueClass<E> {
    final int maxSize = 5;
    private E [] data;
    private int front,count;

    public CSqQueueClass(){
        data = (E[]) new Object[maxSize];
        front = 0;
        count = 0;
    }

    public boolean empty(){
        return count == 0;
    }

    public void push(E e){
        int rear;
        rear = (front+count)%maxSize;
        if (count == maxSize){
            throw new IllegalArgumentException("队列满了");
        }
        rear = (rear + 1) % maxSize;
        data[rear] = e;
        count++;
    }

    public E pop(){
        if (empty()){
            throw new IllegalArgumentException("队列为空");
        }
        count--;
        front = (front+1) % maxSize;
        return (E)data[front];
    }

    public E peek(){
        if (empty()){
            throw new IllegalArgumentException("队列为空");
        }
        return (E)data[(front+1)%maxSize];
    }

    @Override
    public String toString() {
        int rear = (front+count)%maxSize;
        String answer = "";
        if (rear <=front){
            for (int i = front+1;i<maxSize;i++){
                answer += data[i] + " ";
            }
            for (int i = 0;i<=rear;i++){
                answer += data[i] + " ";
            }
        }else {
            for(int i = front+1;i<=rear;i++){
                answer += data[i] + " ";
            }
        }
        return answer;
    }
}

